package cn.com.cetccst.appstore.ui.activity;

import static cn.com.cetccst.appstore.ui.activity.viewmodel.AppStoreViewModel.TYPE_APP_CATEGORY;
import static cn.com.cetccst.appstore.ui.activity.viewmodel.AppStoreViewModel.TYPE_APP_LIST;
import static cn.com.cetccst.appstore.ui.activity.viewmodel.AppStoreViewModel.TYPE_UPGRADE;
import static cn.com.cetccst.appstore.ui.fragment.viewmodel.AppListViewModel.TYPE_NEED_UPGRADE;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.fragment.app.FragmentTransaction;

import cn.com.cetccst.appstore.BR;
import cn.com.cetccst.appstore.R;
import cn.com.cetccst.appstore.databinding.ActivitySearchBinding;
import cn.com.cetccst.appstore.ui.activity.viewmodel.SearchViewModel;
import cn.com.cetccst.appstore.ui.fragment.AppListFragment;
import cn.com.cetccst.appstore.ui.fragment.BaseFragment;
import cn.com.cetccst.appstore.ui.fragment.CategoryListFragment;
import cn.com.cetccst.uicomponents.binding.app.DataBindingConfig;

public class SearchActivity extends BaseActivity<ActivitySearchBinding> {

    public static final String EXTRA_PAGE_TYPE = "page_type";

    public static final String EXTRA_CATEGORY_ID = "category_id";
    public static final String EXTRA_CATEGORY_NAME = "category_name";

    private SearchViewModel viewModel;

    private int currentPageType;

    private String selectedCategoryId;
    private BaseFragment fragment;

    @Override
    protected void initViewModel() {
        viewModel = getActivityScopeViewModel(SearchViewModel.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_search, BR.viewModel, viewModel)
                .addBindingParam(BR.activity, this)
                .addBindingParam(BR.editAction, (TextView.OnEditorActionListener) (textView, actionId, keyEvent) -> {
                    if (actionId == EditorInfo.IME_ACTION_SEND ||
                            (keyEvent != null && keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                        fragment.search(textView.getText().toString().trim());
                    }
                    return false;
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        currentPageType = getIntent().getIntExtra(EXTRA_PAGE_TYPE, TYPE_APP_LIST);
        selectedCategoryId = getIntent().getStringExtra(EXTRA_CATEGORY_ID);
        viewModel.categoryName.setValue(getIntent().getStringExtra(EXTRA_CATEGORY_NAME));
        showFragment();
    }

    public void afterTextChanged(Editable s) {
        viewModel.searchText.setValue(s.toString());
        fragment.search(viewModel.searchText.getValue());
    }

    public int getSearchHint() {
        switch (currentPageType) {
            case TYPE_APP_LIST:
                return R.string.store_search_hint_apps;
            case TYPE_APP_CATEGORY:
                return R.string.store_search_hint_category;
            default:
                return R.string.store_search_hint_upgrade;
        }
    }

    private void showFragment() {
        fragment = null;
        switch (currentPageType) {
            case TYPE_APP_LIST:
                fragment = new AppListFragment();
                if (!TextUtils.isEmpty(selectedCategoryId)) {
                    Bundle bundle = new Bundle();
                    bundle.putString(AppListFragment.EXTRA_CATEGORY_ID, getIntent().getStringExtra(EXTRA_CATEGORY_ID));
                    fragment.setArguments(bundle);
                }
                break;
            case TYPE_APP_CATEGORY:
                fragment = new CategoryListFragment();
                break;
            case TYPE_UPGRADE:
                fragment = new AppListFragment();
                Bundle bundle = new Bundle();
                bundle.putInt(AppListFragment.EXTRA_TYPE, TYPE_NEED_UPGRADE);
                fragment.setArguments(bundle);
                break;
        }
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.fragment_container, fragment).commitNowAllowingStateLoss();
    }

}
